package com.hhit.bigscreenbackmanage.service.impl.jdgl;

import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.entity.jdgl.*;
import com.hhit.bigscreenbackmanage.dao.jdgl.JdglMapper;
import com.hhit.bigscreenbackmanage.service.jdgl.JdglService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class JdglServiceImpl implements JdglService {

    Logger logger = LoggerFactory.getLogger(JdglServiceImpl.class);

    private static String[] cityArr = {"建设分公司", "南京", "苏州", "无锡", "徐州", "常州", "镇江", "扬州", "泰州", "南通", "盐城", "淮安", "宿迁", "连云港"};

    @Autowired
    private JdglMapper jdglMapper;

    /**
     * 重点工程列表
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zdgcxq> getZdgcxq(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zdgcxq> page = new PageUtil<Zdgcxq>();
        List<Zdgcxq> userList =null;
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getZdgcxqCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            userList = jdglMapper.getZdgcxq(map);
            //处理超期情况 字段
//            for (Zdgcxq ndjhEntity:userList) {
//                String cqqk = ndjhEntity.getCqqk();
//                if(!cqqk.equals("N")){
//                    String cqqk1 = cqqk.split("\\.")[0];
//                    //将得到的时间天数转成时间格式
//                    Calendar calendar = new GregorianCalendar(1900,0,-1);
//                    Date d = calendar.getTime();
//                    Date dd = DateUtils.addDays(d,Integer.valueOf(cqqk1));
//                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
//                    cqqk=sdf.format(dd);
//                    ndjhEntity.setCqqk(cqqk+"超期");
//                }else {
//                    ndjhEntity.setCqqk("未超期");
//                }
//            }




            page.setList(userList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("报错啦：" + e.getMessage());
        }
        return page;
    }
    /**
     * 招标管理国招批次列表
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zbglgzpcxq> getZbglgzpcxq(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zbglgzpcxq> page = new PageUtil<Zbglgzpcxq>();
        List<Zbglgzpcxq> userList =null;
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getZbglgzpcxqCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            userList = jdglMapper.getZbglgzpcxq(map);
            page.setList(userList);
        } catch (Exception e) {
            logger.error("报错啦：" + e.getMessage());
        }
        return page;
    }
    /**
     * 招标管理省招物资批次列表
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zbglszwzpcxq> getZbglszwzpcxq(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zbglszwzpcxq> page = new PageUtil<Zbglszwzpcxq>();
        List<Zbglszwzpcxq> userList =null;
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getZbglszwzpcxqCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            userList = jdglMapper.getZbglszwzpcxq(map);
            page.setList(userList);
        } catch (Exception e) {
            logger.error("报错啦：" + e.getMessage());
        }
        return page;
    }
    /**
     * 招标管理省招服务批次列表
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zbglszfwpcxq> getZbglszfwpcxq(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zbglszfwpcxq> page = new PageUtil<Zbglszfwpcxq>();
        List<Zbglszfwpcxq> userList =null;
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getZbglszfwpcxqCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            userList = jdglMapper.getZbglszfwpcxq(map);
            page.setList(userList);
        } catch (Exception e) {
            logger.error("报错啦：" + e.getMessage());
        }
        return page;
    }
    /**
     * 招标管理协议执行批次列表
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zbglxyzxpcxq> getZbglxyzxpcxq(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zbglxyzxpcxq> page = new PageUtil<Zbglxyzxpcxq>();
        List<Zbglxyzxpcxq> userList =null;
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getZbglxyzxpcxqCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            userList = jdglMapper.getZbglxyzxpcxq(map);
            page.setList(userList);
        } catch (Exception e) {
            logger.error("报错啦：" + e.getMessage());
        }
        return page;
    }

    /**
     * 获取年度进度计划-按数量统计
     *
     * @param
     * @return list
     */
    @Override
    public List<Map<String, Object>> getNdjdjhasltj(Map map) {
        return jdglMapper.getNdjdjhasltj(map);
    }

    /**
     * 获取年度进度计划-按规模统计
     *
     * @param
     * @return list
     */
    @Override
    public List<Map<String, Object>> getNdjdjhagmtj(Map map) {
        return jdglMapper.getNdjdjhagmtj(map);
    }

    /**
     * 获取累计开工情况
     *
     * @param
     * @return list
     */
    @Override
    public List<Map<String, Object>> getLjkgqk(Map map) {
        return jdglMapper.getLjkgqk(map);
    }

    /**
     * 获取累计投产情况
     *
     * @param
     * @return list
     */
    @Override
    public List<Map<String, Object>> getLjtcqk(Map map) {
        return jdglMapper.getLjtcqk(map);
    }

    /**
     * 获取在建情况
     *
     * @param
     * @return list
     */
    @Override
    public List<Map<String, Object>> getZjqk(Map map) {
        return jdglMapper.getZjqk(map);
    }

    /**
     * 获取下月计划
     *
     * @param
     * @return list
     */
    @Override
    public List<Map<String, Object>> getXyjh(Map map) {
        return jdglMapper.getXyjh(map);
    }

    /**
     * 重点工程
     *
     * @param
     * @return List
     */
    @Override
    public List<Zdgc> getZdgc(Map map) {
        return jdglMapper.getZdgc(map);
    }
    /**
     * 招标管理
     *
     * @param
     * @return List
     */
    @Override
    public List<Zbgl> getZbgl(Map map) {
        return jdglMapper.getZbgl(map);
    }
    /**
     * 中间echart图
     *
     * @param
     * @return List
     */
    @Override
    public TwoBarEchart getTwoBarEchart(Map map) {
        TwoBarEchart twobarechart = new TwoBarEchart();
        twobarechart.setJsfgs("0");
        twobarechart.setNj("0");
        twobarechart.setSz("0");
        twobarechart.setWx("0");
        twobarechart.setXz("0");
        twobarechart.setCz("0");
        twobarechart.setZj("0");
        twobarechart.setYz("0");
        twobarechart.setTz("0");
        twobarechart.setNt("0");
        twobarechart.setYc("0");
        twobarechart.setHa("0");
        twobarechart.setSq("0");
        twobarechart.setLyg("0");
        List<Map<String, Object>> twobarechartg = jdglMapper.getTwoBarEchart(map);
        for(int i = 0; i < twobarechartg.size(); i++){
            if(twobarechartg.get(i).get("dw").toString().contains("南京")){
                twobarechart.setNj(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("南通")){
                twobarechart.setNt(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("宿迁")){
                twobarechart.setSq(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("常州")){
                twobarechart.setCz(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("建设分公司")){
                twobarechart.setJsfgs(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("徐州")){
                twobarechart.setXz(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("扬州")){
                twobarechart.setYz(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("无锡")){
                twobarechart.setWx(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("泰州")){
                twobarechart.setTz(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("淮安")){
                twobarechart.setHa(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("盐城")){
                twobarechart.setYc(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("苏州")){
                twobarechart.setSz(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("连云港")){
                twobarechart.setLyg(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
            if(twobarechartg.get(i).get("dw").toString().contains("镇江")){
                twobarechart.setZj(""+Math.round(Double.parseDouble(twobarechartg.get(i).get("zhi").toString())));
            }
        }
        return twobarechart;
    }

    @Override
    public List<Map<String,Object>> getUnitProject(Map map) {
        List<Map<String,Object>> result = new ArrayList<>();
        try {
            //获取所有的省公司
            List<NdjhEntity> sgsList = jdglMapper.getUnitProject();
            Map<String,Object> rsultMap = new HashMap<>();
            rsultMap.put("title","国网江苏省电力有限公司");
            rsultMap.put("expand",true);
            if(sgsList.size() > 0){
                List<Map<String,Object>> gsList = new ArrayList<>();
                Map<String,Object> gsMap2 = new HashMap<>();
                List<Map<String,Object>> projectChildren2 = new ArrayList<>();
                for (int i = 0; i <sgsList.size() ; i++) {
                    if(sgsList.get(i).getJsgldw().equals("建设分公司") || sgsList.get(i).getJsgldw().equals("江苏建设分公司")){
                        if(sgsList.get(i).getJsgldw().equals("建设分公司")){
                            gsMap2.put("title",sgsList.get(i).getJsgldw());
                        }

                        //获取工程列表
                        List<NdjhEntity> projectLists = jdglMapper.getProjects(sgsList.get(i).getJsgldw());

                        for (int j = 0; j < projectLists.size(); j++) {
                            Map<String,Object> projectMap = new HashMap<>();
                            projectMap.put("title",projectLists.get(j).getXmmc());
                            projectMap.put("expand",true);
                            projectChildren2.add(projectMap);
                        }
                        if(sgsList.get(i).getJsgldw().equals("江苏建设分公司")){
                            gsMap2.put("children",projectChildren2);
                            gsList.add(gsMap2);
                        }

                    }else{
                        Map<String,Object> gsMap = new HashMap<>();
                        gsMap.put("title",sgsList.get(i).getJsgldw());

                        //获取工程列表
                        List<NdjhEntity> projectLists = jdglMapper.getProjects(sgsList.get(i).getJsgldw());
                        List<Map<String,Object>> projectChildren = new ArrayList<>();
                        for (int j = 0; j < projectLists.size(); j++) {
                            Map<String,Object> projectMap = new HashMap<>();
                            projectMap.put("title",projectLists.get(j).getXmmc());
                            projectMap.put("expand",true);
                            projectChildren.add(projectMap);
                        }
                        gsMap.put("children",projectChildren);
                        gsList.add(gsMap);
                    }

                }
                rsultMap.put("children",gsList);
                result.add(rsultMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取2021年进度计划表格数据
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zdgcxq> getJdjhList(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zdgcxq> page = new PageUtil<Zdgcxq>();
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getJdjhListCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            List<Zdgcxq> userList = jdglMapper.getJdjhList(map);
            page.setList(userList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取2021年进度计划表格数据报错啦：" + e.getMessage());
        }
        return page;
    }

    /**
     * 获取累计开工情况
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zdgcxq> getSyljkgqk(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zdgcxq> page = new PageUtil<Zdgcxq>();
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getSyljkgqkCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            List<Zdgcxq> userList = jdglMapper.getSyljkgqk(map);
            page.setList(userList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取累计开工情况表格数据报错啦：" + e.getMessage());
        }
        return page;
    }

    /**
     * 获取累计投产情况
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zdgcxq> getSyljtcqk(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zdgcxq> page = new PageUtil<Zdgcxq>();
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getSyljtcqkCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            List<Zdgcxq> userList = jdglMapper.getSyljtcqk(map);
            page.setList(userList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取累计投产情况表格数据报错啦：" + e.getMessage());
        }
        return page;
    }

    /**
     * 获取下月计划表格数据
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zdgcxq> getXyjhList(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zdgcxq> page = new PageUtil<Zdgcxq>();
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getXyjhListCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            List<Zdgcxq> userList = jdglMapper.getXyjhList(map);
            page.setList(userList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取下月计划表格数据报错啦：" + e.getMessage());
        }
        return page;
    }

    /**
     * 获取在建情况表格数据
     *
     * @param
     * @return List
     */
    @Override
    public PageUtil<Zdgcxq> getZjqkList(Map map, Integer PAGENO, int PAGESIZE) {
        PageUtil<Zdgcxq> page = new PageUtil<Zdgcxq>();
        try {
            page.pageCount=PAGESIZE;
            page.init(jdglMapper.getZjqkListCount(map),PAGENO);//参数1：总行数，参数2：当前页
            map.put("rowNum",page.getRowNum());//当前页起始行号
            map.put("pageCount",PAGESIZE);//每页行数
            List<Zdgcxq> userList = jdglMapper.getZjqkList(map);
            page.setList(userList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取在建情况表格数据报错啦：" + e.getMessage());
        }
        return page;
    }

    @Override
    public Map<String, Object> queryGdsgszhjhwcqk(Map params) {
        Map<String, Object> out = new HashMap<>();
        List<Map<String, Object>> list = jdglMapper.queryGdsgszhjhwcqk(params);
        out.put("zhjhwcqkData",list);
        return out;
    }

    @Override
    public Map<String, Object> searchJzsydjdwcqkData() {
        Map<String, Object> out = new HashMap<>();
        List<Map<String, Object>> list = jdglMapper.searchJzsydjdwcqkData();
        out.put("jzydJdwcqkData",list.get(0));
        return out;
    }

    @Override
    public Map<String, Object> queryNdjhData(Map params) {
        Map<String, Object> out = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> jhkgtcList = jdglMapper.queryNdjhKgTcData();
        for (Map m:jhkgtcList) {
            map = m;
        }
        List<Map<String, Object>> xlgmOrBdgmList = new ArrayList<>();
        if("1".equals(params.get("xlgm"))){
            xlgmOrBdgmList = jdglMapper.queryNdjhXlData();
        }else if("1".equals(params.get("bdgm"))){
            xlgmOrBdgmList= jdglMapper.queryNdjhBdData();
        }
        for (Map m:xlgmOrBdgmList) {
            map.put("kggm",m.get("kggm"));
            map.put("tcgm",m.get("tcgm"));
        }
        map.put("zjsbdgc",986);
        out.put("ndjhData",map);
        return out;
    }

}
